<?php
class Mstatus extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="status";
    }
    
    public function listAll() {
        $this->db->order_by("status_order", "asc");
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->result_array();
        else return false;
    }
    
    public function listAllLimit($amount, $start) {
        $this->db->order_by("status_order", "asc");
        $this->db->limit($amount, $start);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->result_array();
        else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function getById($id) {
        $this->db->where("status_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getByName($name) {
        $this->db->where("status_name", $name);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function search($name, $amount, $start) {
        $this->db->like("status_name", $name);
        $this->db->limit($amount, $start);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($name) {
        $this->db->like("status_name", $name);
        $this->db->from($this->_table);
        return $this->db->count_all_results();
    }
    
    public function insert($data) {
        $this->db->insert($this->_table, $data);
    }
    
    public function update($data, $id) {
        $this->db->where("status_id", $id);
        $this->db->update($this->_table, $data);
    }


    public function del($id) {
        $this->db->where("status_id", $id);
        $this->db->delete($this->_table);
    }
    
    public function checkName($name, $id="") {
        if($id!="")
            $this->db->where("status_id != ", $id);
        $this->db->where("status_name", $name);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0)
            return false;
        else
            return true;
    }
}
?>
